package com.Ugent.Minerva;

import java.util.ArrayList;

import org.ksoap2.serialization.SoapObject;

public class Facade {
	
	public static int getIcon (int position, ArrayList<String> image){
		//request voor intnummer
		String a = image.get(position).toString();
        int icoonnummer;

        if (a.equals("agenda")){ 
        	icoonnummer = R.drawable.agenda;
        } else  if (a.equals("valves")){
        	icoonnummer = R.drawable.valves;
        	  
        } else  if (a.equals("links")){
        	icoonnummer = R.drawable.links;
        	  
        }else{
        	icoonnummer = R.drawable.documents;
        	  
        }
		return icoonnummer;
	}
	

	
		public static String getDate (int position, ArrayList<String> array){
			String [] date = array.get(position).toString().split("T"); 
			String result = date[0];
			return result;
		}
		public static String getDate2 (String date){
			String [] date2 = date.split("T"); 
			String result = date2[0];
			return result;
		}
		public static int getYear (String date){
			String [] datum = date.split("-");
			String year1 = datum[0];
			int year = Integer.parseInt(year1);
			return year;
			
		}
		public static int getMonth (String date){
			String [] datum = date.split("-");
			String month1 = datum[1];
			int month = Integer.parseInt(month1);
			return month;
			
		}
		public static int getDay (String date){
			String [] datum = date.split("-");
			String day1 = datum[2];
			int day = Integer.parseInt(day1);
			return day;
			
		}
		public static String getHourDate (String date){
			String [] hour = date.toString().split("T"); 
			
			String hourTime= hour[1];
			String result = hourTime.substring(0, (hourTime.length()-3));
			return result;
		}
		public static int getYearAdd (String date){
			
			String year1 = date.substring(0, 4);
			int year = Integer.parseInt(year1);
			return year;
			
		}
		public static int getMonthAdd (String date){
			String [] datum = date.split("T");
			String month1 = datum[0];
			String month2 = month1.substring(5, 7);
			
			int month = Integer.parseInt(month2);
			return month;
			
		}
		public static int getDayAdd (String date){
			String [] datum = date.split("T");
			String day1 = datum[0];
			String day2 = day1.substring(8);
			int day = Integer.parseInt(day2);
			return day;
			
		}
		public static int getHour (String date){
			String [] hour = date.toString().split("T"); 
			
			String hourTime= hour[1];
			String result = hourTime.substring(0, (hourTime.length()-6));
			int hour1 = Integer.parseInt(result);
			return hour1;
		}
		public static int getMinute (String date){
			String [] hour = date.toString().split("T"); 
			
			String hourTime= hour[1];
			String result = hourTime.substring(3, (hourTime.length()-3));
			int minute = Integer.parseInt(result);
			return minute;
		}
		public static int getSeconde (String date){
			String [] hour = date.toString().split("T"); 
			
			String hourTime= hour[1];
			String result = hourTime.substring(6);
			int seconde = Integer.parseInt(result);
			return seconde;
		}
		
		
		
		
		public static ArrayList<Course> getCourses(String officialCode){
			ArrayList<Course> courseList = new ArrayList<Course>();
			//soap
			SoapObject resultList = (SoapObject)Soap.getCourseList(officialCode);
			
			//manueel parsen en toevoegen naar CourseObject
			String [] testValues = new String[resultList.getPropertyCount()];
	        String [] course;     
	        
	        if (courseList.isEmpty()){
	        for(int i= 0; i< resultList.getPropertyCount(); i++){
	            testValues[i] = resultList.getProperty(i).toString(); 
	            int length = testValues[i].length();
	            String totaal = testValues[i].substring(11,(length-2));
	            course = totaal.split(";");
	            
	        	Course c = new Course();
	            for (int j=0; j<course.length; j++){
	            
	            if (j==0){
	                int k= course[j].indexOf("=");
	                String a= course[j].substring(k+1);
	                c.setCode(a);
	                  
	              }
	            
	            if (j==1){
	                int k= course[j].indexOf("=");
	                String a= course[j].substring(k+1);
	                c.setTitle(a);
	                  
	              }
	            
	            if (j==2){
	                int k= course[j].indexOf("=");
	                String a= course[j].substring(k+1);
	                c.setVisualCode(a);
	                  
	              }
	            if (j==3){
	                int k= course[j].indexOf("=");
	                String a= course[j].substring(k+1);
	                c.setTutor(a);
	                  
	              } 
	            if (j==4){
	                int k= course[j].indexOf("=");
	                String a= course[j].substring(k+1);
	                c.setCourseLanguage(a);
	                  
	              }
	            if (j==5){
	                int k= course[j].indexOf("=");
	                String a= course[j].substring(k+1);
	                c.setStatus(a);
	                  
	                
	              }
	            
	            
	            }     
	            courseList.add(c);
	            
	            
	        }
	        }
			return courseList;
			
			
	    	
	    }
		
		public static ArrayList<Tool> getTools (int position, ArrayList<Course> courseList){
			ArrayList<Tool> toolList = new ArrayList<Tool>();
			//soap
			SoapObject tools = (SoapObject)Soap.getActiveTools(courseList.get(position).getCode());
			//manueel parsen en toevoegen naar ToolObject
			String [] testValues = new String[tools.getPropertyCount()];
		    String [] tool;

		    if (toolList.isEmpty()){
		    for(int i= 0; i< tools.getPropertyCount(); i++){
		        testValues[i] = tools.getProperty(i).toString(); 
		        int length = testValues[i].length();
		        String totaal = testValues[i].substring(9,(length-2));
		        tool = totaal.split(";");
		        Tool t = new Tool();
		        for (int j=0; j<tool.length; j++){
		                
		        if (j==0){
		                int k= tool[j].indexOf("=");
		                String a= tool[j].substring(k+1);
		                t.setName(a);
		        }
		        if (j==1){
		                int k= tool[j].indexOf("=");
		                String a= tool[j].substring(k+1);
		                t.setImage(a);
		        }
		       
		        
		    }
		        toolList.add(t);
		  
		  }
			
		    }   
		    return toolList;  
		}
		
		public static ArrayList<Announcement> getAnnouncements (Course a){
			ArrayList<Announcement> announcements = new ArrayList<Announcement>();
			//soap
			SoapObject result = (SoapObject) Soap.getToolContent(a.getCode(), "announcement");

			//manueel parsen en toevoegen naar AnnouncementObject
			String [] testValues = new String[result.getPropertyCount()];
			String [] announcement;
			
			if (announcements.isEmpty()){
			for(int i = (result.getPropertyCount()-1); i>=0; i--){	
				testValues[i] = result.getProperty(i).toString(); 
				int length = testValues[i].length();
				String totaal = testValues[i].substring(9,(length-2));
				announcement = totaal.split(";");
				Announcement n = new Announcement();
				for (int j=0; j<announcement.length; j++){
					if (j==1){
						int k= announcement[j].indexOf("=");
						String b= announcement[j].substring(k+1);
						n.setReference(b);
					}
					if (j==2){
						int k= announcement[j].indexOf("=");
						String b= announcement[j].substring(k+1);
						n.setTitle(b);
					}
					if (j==3){
						int k= announcement[j].indexOf("=");
						String b= announcement[j].substring(k+1);
						n.setAnnouncement(b);
					}
        
				 
        }
				announcements.add(n);
				}
}
		
			
			return announcements;
			}

public static ArrayList<Link> getLinks (Course a){
	ArrayList<Link> links = new ArrayList<Link>();
	//soap
	SoapObject result = (SoapObject) Soap.getToolContent(a.getCode(), "link");

	//manueel parsen en toevoegen naar AnnouncementObject
	String [] testValues = new String[result.getPropertyCount()];
	String [] link;
	
	if (links.isEmpty()){
	for(int i = (result.getPropertyCount()-1); i>=0; i--){
		testValues[i] = result.getProperty(i).toString(); 
		int length = testValues[i].length();
		String totaal = testValues[i].substring(9,(length-2));
		link = totaal.split(";");
		Link l = new Link();
		for (int j=0; j<link.length; j++){
			if (j==1){
				int k= link[j].indexOf("=");
				String b= link[j].substring(k+1);
				l.setReference(b);
			}
			if (j==2){
				int k= link[j].indexOf("=");
				String b= link[j].substring(k+1);
				l.setTitle(b);
			}
			if (j==3){
				int k= link[j].indexOf("=");
				String b= link[j].substring(k+1);
				l.setLink(b);
			}
			if (j==4){
				int k= link[j].indexOf("=");
				String b= link[j].substring(k+1);
				l.setDescription(b);

			}

                
}
		links.add(l);

}
}
	return links;
	}

public static ArrayList<CalendarEvent> getCalendar (Course a){
	ArrayList<CalendarEvent> calendar = new ArrayList<CalendarEvent>();
	//soap
	SoapObject result = (SoapObject) Soap.getToolContent(a.getCode(), "calendar_event");

	//manueel parsen en toevoegen naar AnnouncementObject
	String [] testValues = new String[result.getPropertyCount()];
	String [] agenda;
	
	if (calendar.isEmpty()){
	for(int i = (result.getPropertyCount()-1); i>=0; i--){
		testValues[i] = result.getProperty(i).toString(); 
		int length = testValues[i].length();
		String totaal = testValues[i].substring(9,(length-2));
		agenda = totaal.split(";");
		CalendarEvent c = new CalendarEvent();
		for (int j=0; j<agenda.length; j++){
			if (j==1){
				int k= agenda[j].indexOf("=");
				String b= agenda[j].substring(k+1);
				c.setReference(b);
			}
			if (j==2){
				int k= agenda[j].indexOf("=");
				String b= agenda[j].substring(k+1);
				c.setTitle(b);
			}
			if (j==3){
				int k= agenda[j].indexOf("=");
				String b= agenda[j].substring(k+1);
				c.setDescription(b);
			}
			if (j==4){
				int k= agenda[j].indexOf("=");
				String b= agenda[j].substring(k+1);
			    c.setStart(b);
			}
			if (j==5){
				int k= agenda[j].indexOf("=");
				String b= agenda[j].substring(k+1);
			    c.setEnd(b);

			}

                
}
		calendar.add(c);

}
}
	return calendar;
	}


}